# -*- coding: UTF-8 -*-
from PIL import Image
import os
import random
import time
import datetime
import utils,cnn

param = {}

def LoadParam():
    param["captcha_path"] = utils.GetINIData("config.ini","CONFIG","captcha_path")
    param["model_path"] = utils.GetINIData("config.ini","CONFIG","model_path")
    param["captcha_image_w"] = int(utils.GetINIData("config.ini","CONFIG","captcha_image_w"))
    param["captcha_image_h"] = int(utils.GetINIData("config.ini","CONFIG","captcha_image_h"))
    param["captcha_len"] = int(utils.GetINIData("config.ini","CONFIG","captcha_len"))
    param["number"] = utils.GetINIData("config.ini","CONFIG","number")
    param["layer_width"] = int(utils.GetINIData("config.ini","CONFIG","layer_width"))
    param["layer_height"] = int(utils.GetINIData("config.ini","CONFIG","layer_height"))
    param["range_value"] = int(utils.GetINIData("config.ini","CONFIG","range_value"))
    param["step_value"] = int(utils.GetINIData("config.ini","CONFIG","step_value"))
    param["acc"] = float(utils.GetINIData("config.ini","CONFIG","acc"))
    param["batchSize"] = int(utils.GetINIData("config.ini","CONFIG","batchSize"))
    param["keep_prob"] = float(utils.GetINIData("config.ini","CONFIG","keep_prob"))
    param["train_image_precent"] = float(utils.GetINIData("config.ini","CONFIG","train_image_precent"))
    param["char_set_len"] = len(param["number"])
    param["image_name_len"] = int(utils.GetINIData("config.ini","CONFIG","image_name_len"))
def Main():
    LoadParam()
    TRAINING_IMAGE_NAME = []    #训练集，用于训练的验证码图片的文件名
    VALIDATION_IMAGE_NAME = []  #验证集，用于模型验证的验证码图片的文件名
    image_filename_list, total = utils.get_image_file_name_no_verify(param['captcha_path'])

    random.seed(time.time())
    #打乱顺序
    random.shuffle(image_filename_list)
    trainImageNumber = int(total * param['train_image_precent'])
    #分成测试集
    TRAINING_IMAGE_NAME = image_filename_list[ : trainImageNumber]
    #和验证集
    VALIDATION_IMAGE_NAME = image_filename_list[trainImageNumber : ]
    #print(TRAINING_IMAGE_NAME)
    cnn.set_param(param)
    cnn.Init()
    cnn.train_first(TRAINING_IMAGE_NAME,VALIDATION_IMAGE_NAME)
    print('Training finished')


Main()
